package com.xhwl.centiotdata.handler.passengerflow.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xhwl.centiotdata.handler.passengerflow.PassengerFlowIntegrationFactory;
import com.xhwl.centiotdata.handler.passengerflow.PassengerFlowIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.dahua.PassengerFlowDTO;
import com.xhwl.centiotdata.pojo.dto.passengerflow.PassengerFlowIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.ICanteenPassengerFlowService;
import com.xhwl.centiotdata.suppliersystem.dahuaIcc.PassengerFlowHandler;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.exception.DataException;
import com.xhwl.common.pojo.cent.canteen.CanteenPassengerFlow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author： zhouxudong
 * @Description：
 * @Date 2023-07-06
 */
@Service
@Slf4j
public class PassengerFlowIntegrationICCHandler extends PassengerFlowIntegrationHandler {

    @Autowired
    private PassengerFlowHandler passengerFlowHandler;

    @Autowired
    private ICanteenPassengerFlowService canteenPassengerFlowService;

    @Override
    public void afterPropertiesSet() throws Exception {
        PassengerFlowIntegrationFactory.register(ProductEnum.DH_PASSENGER_FLOW.getId(), this);
    }

    @Override
    public List<PassengerFlowDTO> getStaticFlowRealTimeDataByICC(PassengerFlowIntegrationConfigDTO configDTO, Integer projectId, Integer pageNum, Integer pageSize, Integer reId) {
        if (configDTO != null) {
            return passengerFlowHandler.passengerFlowDTOS(configDTO, pageNum, pageSize, reId);
        }
        return null;
    }


    @Override
    public void syncStaticFlowData(PassengerFlowIntegrationConfigDTO passengerFlowIntegrationConfigDTO) {
        try {
            passengerFlowIntegrationConfigDTO.getRegionConfigs().forEach(passengerFlowRegionConfig -> {
                if (passengerFlowRegionConfig.getRegionIds() != null && passengerFlowRegionConfig.getRegionIds().size() != 0) {
                    Integer localId = passengerFlowRegionConfig.getLocalId();
                    passengerFlowRegionConfig.getRegionIds().forEach(reId -> {
                        List<PassengerFlowDTO> list = this.getStaticFlowRealTimeDataByICC(
                                passengerFlowIntegrationConfigDTO,passengerFlowIntegrationConfigDTO.getProjectId(),
                                1, 1, reId);
                        if (CollectionUtil.isNotEmpty(list)) {
                            PassengerFlowDTO passengerFlowDTO = list.get(0);
                            CanteenPassengerFlow canteenPassengerFlow = new CanteenPassengerFlow();
                            canteenPassengerFlow.setProjectId(passengerFlowIntegrationConfigDTO.getProjectId());
                            canteenPassengerFlow.setCanteenId(localId);
                            canteenPassengerFlow.setInCount(passengerFlowDTO.getInCount());
                            canteenPassengerFlow.setOutCount(passengerFlowDTO.getOutCount());
                            canteenPassengerFlow.setIsDeleted(false);
                            canteenPassengerFlow.setRegionId(reId);
                            canteenPassengerFlow.setTotalInCount(passengerFlowDTO.getTotalInCount());
                            canteenPassengerFlow.setTotalOutCount(passengerFlowDTO.getTotalOutCount());
                            int innerRealCount = passengerFlowDTO.getTotalInCount() - passengerFlowDTO.getTotalOutCount();
                            canteenPassengerFlow.setInnerRealCount(Math.max(innerRealCount, 0));
                            canteenPassengerFlowService.saveSingleData(canteenPassengerFlow);
                        }
                    });
                }
            });
        } catch (Exception e) {
            throw new DataException("第三方数据异常");
        }

    }
}
